import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

from utils.makefigure import show_line_and_data, show_loss

X, Y = make_blobs(n_samples=100,
                  n_features=2,
                  centers=2,
                  cluster_std=1,
                  # center_box=[(-5,-10), (5, 10), (5, 20),],
                  random_state=None)

data_x = np.hstack((X, np.ones((100, 1))))
a = data_x.shape
data_x = data_x[..., np.newaxis]

data_y = Y * 2 - 1

# w初始化
w = np.mat(np.random.random((3, 1)))

u = 0.001

# 分类完成标志
okflag = False
# 循环次数
times = 0
losses = []
while not okflag and times < 100:
    times += 1
    okflag = True
    loss = 0
    for data, y in zip(data_x, data_y):
        result = np.dot(data.T, w) * y
        result = result[0, 0]
        if result < 0:
            okflag = False
            loss += result
            w = w + data * y * u
    losses.append(loss)
show_line_and_data(w, X, Y)

show_loss(losses)
print(times)
